package com.yunhe.estatemanage.service.impl;
import com.yunhe.common.constant.ReminderConfigurationConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.estatemanage.constant.EstateManageConstant;
import com.yunhe.estatemanage.domain.IncidentSatisfactionTable;
import com.yunhe.estatemanage.domain.ReportRepair;
import com.yunhe.estatemanage.repository.IncidentSatisfactionTableRepository;
import com.yunhe.estatemanage.service.IncidentSatisfactionTableService;
import com.yunhe.estatemanage.service.ReportRepairService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yunhe.common.util.CollectionUtil;

import java.util.List;

/**
 * @description 事件满意度表
 * @author misty
 * @date 2021-09-06
 */
@Service
public class IncidentSatisfactionTableServiceImpl implements IncidentSatisfactionTableService {

    @Autowired
    private IncidentSatisfactionTableRepository incidentSatisfactionTableRepository;

    @Autowired
    private ReportRepairService reportRepairService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createIncidentSatisfactionTable (IncidentSatisfactionTable incidentSatisfactionTable) throws ArgumentErrorException {
        ReportRepair reportRepair = reportRepairService.findById(incidentSatisfactionTable.getReportForRepairId());
        if(reportRepair == null){
            throw new ArgumentErrorException("请选择正确的报事报修数据！");
        }
        if(!EstateManageConstant.Current_Status_Type.Completed.value().equals(reportRepair.getCurrentStatus())){
            throw new ArgumentErrorException("请选择已完成的报事报修数据进行评价！");
        }
        //判读是否已经评价过
        List<IncidentSatisfactionTable> incidentSatisfactionTableList = incidentSatisfactionTableRepository.
                searchIncidentSatisfactionTableByReportForRepairId(reportRepair.getId());
        if(!CollectionUtil.isEmpty(incidentSatisfactionTableList)){
            throw new ArgumentErrorException("已评价不可重复评价！");
        }
        incidentSatisfactionTable.setOfficeId(reportRepair.getOfficeId());
        incidentSatisfactionTable.setContactPersonId(reportRepair.getContactPersonId());
        incidentSatisfactionTable.setStationId(reportRepair.getStationId());
        incidentSatisfactionTable.setCompanyId(reportRepair.getCompanyId());
        incidentSatisfactionTable.setOrgId(reportRepair.getOrgId());
        return incidentSatisfactionTableRepository.createIncidentSatisfactionTableSelective(incidentSatisfactionTable);
    }


    @Override
    public int deleteIncidentSatisfactionTable(int id) {
        return incidentSatisfactionTableRepository.deleteIncidentSatisfactionTableByPrimaryKey(id);
    }


    @Override
    public int updateIncidentSatisfactionTable(IncidentSatisfactionTable incidentSatisfactionTable) {
        return incidentSatisfactionTableRepository.updateIncidentSatisfactionTableByPrimaryKeySelective(incidentSatisfactionTable);
    }


    @Override
    public IncidentSatisfactionTable findIncidentSatisfactionTableById(int id) {
        return incidentSatisfactionTableRepository.searchIncidentSatisfactionTableByPrimaryKey(id);
    }



}